package com.ecar.apm.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.ecar.apm.model.EchartsPie;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ecar.apm.model.HttpRequest;
import com.ecar.apm.service.HttpRequestService;
import com.ecar.apm.service.HttpSequenceService;

@Controller
public class ApiMonitorController {

	@Autowired
	private HttpSequenceService httpSequenceService;
	
	@Autowired
	private HttpRequestService httpRequestService;
	
    @RequestMapping("/")
    public String main(ModelMap map) {
		List<Map<String,Object>> list = httpSequenceService.getMonitorList();
		map.addAttribute("monitorList", list);
    	return "monitor_list";
    }
    
    @RequestMapping("/monitorList")
    public String monitorList(ModelMap map) {
		List<Map<String,Object>> list = httpSequenceService.getMonitorList();
		map.addAttribute("monitorList", list);
    	return "monitor_list";
    }

    @RequestMapping("/monitorLog")
    public String monitorLog(ModelMap map, HttpServletRequest request) {
    	String guid = request.getParameter("guid");
    	String name = request.getParameter("name");
		List<Map<String,Object>> list = httpSequenceService.getLogByGuid(guid);
		map.addAttribute("sequencelogs", list);
		map.addAttribute("sequenceName", name);
    	return "monitor_log";
    }
    
    
    @RequestMapping({"/monitorLogEcharts"})
    public String monitorLogEcharts(ModelMap map, HttpServletRequest request)
    {
      String guid = request.getParameter("guid");
      String name = request.getParameter("name");
      HttpRequest httpRequest = new HttpRequest();
      httpRequest = this.httpRequestService.getHttpRequestBypGuid(guid);
      String url = httpRequest.getUrl();
      
      List<Map<String, Object>> logList = this.httpSequenceService.getLogByGuidForEcharts(guid);
      List<String> myNameList = new ArrayList();
      List<Integer> myValueList = new ArrayList();
      for (Map<String, Object> myMap : logList) {
        for (Map.Entry<String, Object> m : myMap.entrySet()) {
          if (((String)m.getKey()).equals("name")) {
            myNameList.add(m.getValue().toString());
          } else {
            myValueList.add(Integer.valueOf(Integer.parseInt(m.getValue().toString())));
          }
        }
      }
      map.addAttribute("myNameList", myNameList);
      map.addAttribute("myValueLogList", myValueList);
      map.addAttribute("sequenceName", name);
      map.addAttribute("sequenceUrl", url);
      
      List<Map<String, Object>> logPie = this.httpSequenceService.getLogByGuidForEchartsPie(guid);
      List<EchartsPie> logPieList = new ArrayList();
      for (Map<String, Object> myMap : logPie)
      {
        String keyName = "";
        int keyValue = 0;
        for (Map.Entry<String, Object> m : myMap.entrySet()) {
          if (((String)m.getKey()).equals("name")) {
            switch (m.getValue().toString())
            {
            case "10": 
              keyName = "10ms以内";
              break;
            case "20": 
              keyName = "10~50ms";
              break;
            case "30": 
              keyName = "50~100ms";
              break;
            case "40": 
              keyName = "100ms以上";
              break;
            }
          } else {
            keyValue = Integer.parseInt(m.getValue().toString());
          }
        }
        EchartsPie mypie = new EchartsPie();
        mypie.setName(keyName);
        mypie.setValue(keyValue);
        logPieList.add(mypie);
      }
      map.addAttribute("myLogPieList", logPieList);
      
      return "monitor_log_echarts";
    }

	@ResponseBody
    @RequestMapping(value="/apiLog",method=RequestMethod.GET)
    public List<Map<String, Object>>  apiLog(HttpServletRequest request, HttpServletResponse response){
		String id = request.getParameter("id");
		List<Map<String, Object>> list = httpRequestService.getHttpRequestLogByPid(id);
        return list;
    }
	
	@ResponseBody
    @RequestMapping(value="/enableMonitor",method=RequestMethod.GET)
    public boolean  enableMonitor(HttpServletRequest request, HttpServletResponse response){
		String guid = request.getParameter("guid");
		String enabled = request.getParameter("enabled");
		boolean b;
		if("true".equals(enabled)){
			b = httpRequestService.disableMonitor(guid);
		}else{
			b = httpRequestService.enableMonitor(guid);
		} 
        return b;
    }	
	
	@ResponseBody
    @RequestMapping(value="/deleteMonitor",method=RequestMethod.GET)
    public boolean  deleteMonitor(HttpServletRequest request, HttpServletResponse response){
		String guid = request.getParameter("guid");
		boolean b = httpRequestService.deleteMonitor(guid);
        return b;
    }
	
}
